import Vue, { DirectiveOptions } from 'vue'
import App from './App.vue'
import router from '@/router'
import store from './store'
// 标准html5浏览器默认样式，比如body-margin:0,原始浏览器margin:8px 等等
import 'normalize.css'
// import './registerServiceWorker';
import '@/styles/element-variables.scss'
import ElementUI from 'element-ui'
import '@/styles/new-element.scss'
import '@/styles/index.scss'
import { AppModule } from '@/store/modules/app'
import * as directives from '@/directives'
import * as filters from '@/filters'
import settings from './settings'
import SvgIcon from 'vue-svgicon'
import '@/icons/components'
import { get } from 'lodash'
import { codemirror } from 'vue-codemirror'
import micro from '@/micro'
import 'codemirror/lib/codemirror.css'

Vue.config.devtools = process.env.NODE_ENV === 'development'

Vue.use(codemirror)

Vue.config.productionTip = false

Vue.use(ElementUI, {
  size: AppModule.size // Set element-ui default size
})
// 统一设置svg-icon风格
Vue.use(SvgIcon, {
  tagName: 'svg-icon',
  defaultWidth: '1em',
  defaultHeight: '1em'
})

Vue.prototype.settings = settings
Vue.prototype.$_get = get
// Register global directives
Object.keys(directives).forEach(key => {
  Vue.directive(key, (directives as { [key: string]: DirectiveOptions })[key])
})
// Register global filter functions
Object.keys(filters).forEach(key => {
  Vue.filter(key, (filters as { [key: string]: Function })[key])
})
import '@/permission'

// 子应用配置开始

declare global {
  interface Window {
    __POWERED_BY_QIANKUN__: boolean
    __INJECTED_PUBLIC_PATH_BY_QIANKUN__: any
  }
}

micro.render = (props: any) => {
  const { container } = props
  micro.instance = new Vue({
    router,
    store,
    render: h => h(App)
  }).$mount(
    container ? container.querySelector('#risksystemApp') : '#risksystemApp'
  )
}

if (!window.__POWERED_BY_QIANKUN__) {
  micro.render({})
} else {
  // runtime publicPath 主要解决的是微应用动态载入的 脚本、样式、图片 等地址不正确的问题
  console.log(window.__INJECTED_PUBLIC_PATH_BY_QIANKUN__)
  __webpack_public_path__ = window.__INJECTED_PUBLIC_PATH_BY_QIANKUN__
}

export async function bootstrap() {
  console.log('bootstrap')
  micro.bootstrap()
}

export async function mount(props: any) {
  console.log('mount')
  micro.mount(props)
}

export async function unmount() {
  micro.unmount()
}
